package cn.iocoder.yudao.module.member.dal.redis.authcode;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.module.member.dal.redis.RedisKeyConstants.AUTH_CODE;
import static cn.iocoder.yudao.module.member.dal.redis.RedisKeyConstants.AUTH_CODE_EXPIRATION;

/**
 * {@link AuthCodeRedisDAO} 的 RedisDAO
 *
 * @author 芋道源码
 */
@Repository
public class AuthCodeRedisDAO {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public String get(String username) {
        String redisKey = formatKey(username);
        return stringRedisTemplate.opsForValue().get(redisKey);
    }

    public void set(String username, String authCode) {
        String redisKey = formatKey(username);
        stringRedisTemplate.opsForValue().set(redisKey, authCode, AUTH_CODE_EXPIRATION, TimeUnit.MINUTES);
    }

    public void delete(String username) {
        String redisKey = formatKey(username);
        stringRedisTemplate.delete(redisKey);
    }

    private static String formatKey(String username) {
        return String.format(AUTH_CODE, username);
    }

}
